// @ts-ignore
import Taro, { Events } from "@tarojs/taro";

// 审核状态
export const AUDIT_TAG_STATE = {
  1: {
    color: "#2A64F4",
    label: "待审核",
  },
  2: {
    color: "#f3812e",
    label: "审核中",
  },
  3: {
    color: "#10B809",
    label: "通过",
  },
  4: {
    color: "#FF4026",
    label: "拒绝",
  },
  7: {
    color: "#2A64F4",
    label: "待提交",
  },
  8: {
    color: "#666666",
    label: "已取消",
  },
};

// 出行状态
export const TRIP_TAG_STATE = {
  1: {
    color: "#2A64F4",
    label: "待提交",
  },
  2: {
    color: "#2A64F4",
    label: "待审核",
  },
  3: {
    color: "#2A64F4",
    label: "待处理",
  },
  4: {
    color: "#f3812e",
    label: "处理中",
  },
  5: {
    color: "#10B809",
    label: "处理完成",
  },
  6: {
    color: "#FF4026",
    label: "暂不处理",
  },
  7: {
    color: "#2A64F4",
    label: "撤回",
  },
};
export const FAULT_TAG_STATE = {
  1: {
    color: "#2A64F4",
    label: "待出行",
  },
  2: {
    color: "#f3812e",
    label: "出行中",
  },
  3: {
    color: "#10B809",
    label: "已完成",
  },
};

// 调度状态
export const SCHEDULING_TAG_STATE = {
  1: {
    color: "#10B809",
    label: "已调度",
  },
  2: {
    color: "#2A64F4",
    label: "待调度",
  },
};
// 防抖函数
export function debounce(func: Function, delay: number | undefined) {
  let timer: any = null;
  return function () {
    clearTimeout(timer);
    timer = setTimeout(() => {
      // @ts-ignore
      func.apply(this, arguments);
    }, delay);
  };
}

// 获取元素数据
export function queryElementInfo(ele: string) {
  return new Promise((resolve) => {
    const query = Taro.createSelectorQuery();
    query
      .select(ele)
      .boundingClientRect((rec) => {
        resolve(rec);
      })
      .exec();
  });
}

export function timestampToTime(timestamp: number) {
  let splicer = "-";
  // 时间戳为10位需*1000，时间戳为13位不需乘1000
  let date = new Date(Number(timestamp));
  let Y = date.getFullYear() + splicer;
  let M =
    (date.getMonth() + 1 < 10
      ? "0" + (date.getMonth() + 1)
      : date.getMonth() + 1) + splicer;
  let D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
  let h = date.getHours() + ":";
  let m = date.getMinutes() + ":";
  let s = date.getSeconds();
  return Y + M + D + h + m + s;
}

export function timeConversion(timestamp: any, sign?: any) {
  let splicer = "-";
  // 时间戳为10位需*1000，时间戳为13位不需乘1000
  let date = new Date(timestamp);
  let Y = date.getFullYear() + splicer;
  let M =
    (date.getMonth() + 1 < 10
      ? "0" + (date.getMonth() + 1)
      : date.getMonth() + 1) + splicer;
  let D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
  let h = date.getHours() + ":";
  let m = date.getMinutes() + ":";
  let s = date.getSeconds();
  return sign == 2 ? Y + M + D : Y + M + D + h + m + s;
}

// 校验是否有权限
export function getHasPermissions(data: any[], code: string) {
  const systemMenu = data || [];
  return !!systemMenu?.find((menu: any) => !code || menu?.menuCode === code);
}

//
export function getDateStr(addDayCount: number) {
  let dd = new Date();
  dd.setDate(dd.getDate() + addDayCount); //获取AddDayCount天后的日期
  let y = dd.getFullYear();
  let m = dd.getMonth() + 1; //获取当前月份的日期
  let d = dd.getDate();
  return `${y}-${`${m}`?.length === 1 ? `0${m}` : m}-${d}`;
}

// 日期转换星期
export function getDayOfWeek(dateString:string) {
  const days = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
  const date = new Date(dateString);
  const dayIndex = date.getDay();
  return days[dayIndex];
}

export function getStatusBarHeight(){
  Taro.getSystemInfoAsync({
    success(res) {
      return res.statusBarHeight
    }
  })
}

export const events = new Events();
